package com.drinks.yiniang.adapter.listview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.drinks.yiniang.R;
import com.drinks.yiniang.entity.message.ChatJson;
import com.hansen.library.ui.widget.image.CircleImageView;
import com.drinks.yiniang.utils.GlideUtils;
import com.hansen.library.utils.CommonUtils;

import java.util.List;

/**
 * Created by han on 2018/11/1 0001.
 */

public class ServiceChatAdapter extends BaseAdapter {

    public static final String TYPE_CHAT_MSG_LEF = "2";
    public static final String TYPE_CHAT_MSG_RIGHT = "1";

    private final int TYPE_LEFT = 0x0;
    private final int TYPE_RIGHT = 0x01;
    private final int TYPE_SEND_TIME = 0x03;
    private final int ITEM_COUNT = 4;

    private Context mContext;

    private List<ChatJson.ChatList> mList;

    private LayoutInflater inflater;

    //是否是和客服聊天
    private boolean isCustomerServiceChat;

    public ServiceChatAdapter(Context context, List<ChatJson.ChatList> mList) {
        this.mContext = context;
        this.mList = mList;

        inflater = LayoutInflater.from(mContext);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ChatJson.ChatList itemData = mList.get(position);

        switch (getItemViewType(position)) {
            case TYPE_LEFT:
                ViewLeftHolder mLeftHolder = new ViewLeftHolder();

                if (convertView == null) {
                    convertView = inflater.inflate(R.layout.item_chat_left, null);

                    mLeftHolder.tv_chat_left_name = convertView.findViewById(R.id.tv_chat_left_name);
                    mLeftHolder.tv_chat_left_content = convertView.findViewById(R.id.tv_chat_left_content);

                    mLeftHolder.iv_chat_left_avatar = convertView.findViewById(R.id.iv_chat_left_avatar);

                    convertView.setTag(mLeftHolder);
                } else {
                    mLeftHolder = (ViewLeftHolder) convertView.getTag();
                }

                GlideUtils.loadAvatar(mContext, mLeftHolder.iv_chat_left_avatar, isCustomerServiceChat ? itemData.getKefu_avatar() : itemData.getAdverse_avatar());

                mLeftHolder.tv_chat_left_name.setText(isCustomerServiceChat ? itemData.getKefu_username() : itemData.getAdverse_username());
                mLeftHolder.tv_chat_left_content.setText(itemData.getContent());
                break;
            case TYPE_RIGHT:
                ViewRightHolder mRightHolder = new ViewRightHolder();

                if (convertView == null) {
                    convertView = inflater.inflate(R.layout.item_chat_right, null);

                    mRightHolder.tv_chat_right_name = convertView.findViewById(R.id.tv_chat_right_name);
                    mRightHolder.tv_chat_right_content = convertView.findViewById(R.id.tv_chat_right_content);
                    mRightHolder.iv_chat_right_avatar = convertView.findViewById(R.id.iv_chat_right_avatar);

                    convertView.setTag(mRightHolder);
                } else {
                    mRightHolder = (ViewRightHolder) convertView.getTag();
                }

                GlideUtils.loadAvatar(mContext, mRightHolder.iv_chat_right_avatar, itemData.getMy_avatar());

                mRightHolder.tv_chat_right_name.setText(itemData.getMy_username());
                mRightHolder.tv_chat_right_content.setText(itemData.getContent());
                break;
            case TYPE_SEND_TIME:
                ViewTimeHolder mTimeHolder = new ViewTimeHolder();

                if (convertView == null) {

                    convertView = inflater.inflate(R.layout.item_chat_send_time, null);

                    mTimeHolder.tv_chat_send_time = convertView.findViewById(R.id.tv_chat_send_time);

                    convertView.setTag(mTimeHolder);
                } else {
                    mTimeHolder = (ViewTimeHolder) convertView.getTag();
                }

                mTimeHolder.tv_chat_send_time.setText(isCustomerServiceChat ? itemData.getSend_time() : itemData.getCreated_at());
                break;
            default:
                break;
        }

        return convertView;
    }

    @Override
    public int getCount() {
        return CommonUtils.getListSize(mList);
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public int getItemViewType(int position) {
        //type 2,类型 1我发送的 2对方发送到, 为空表示显示发送时间
        return TYPE_CHAT_MSG_LEF.equals(mList.get(position).getType()) ? TYPE_LEFT : TYPE_CHAT_MSG_RIGHT.equals(mList.get(position).getType()) ? TYPE_RIGHT : TYPE_SEND_TIME;
    }

    @Override
    public int getViewTypeCount() {
        return ITEM_COUNT;
    }

    public void setIsCustomerServiceChat(boolean isCustomerServiceChat) {
        this.isCustomerServiceChat = isCustomerServiceChat;
    }

    class ViewLeftHolder {
        TextView tv_chat_left_name;
        TextView tv_chat_left_content;

        CircleImageView iv_chat_left_avatar;
    }

    class ViewRightHolder {
        TextView tv_chat_right_name;
        TextView tv_chat_right_content;

        CircleImageView iv_chat_right_avatar;
    }

    class ViewTimeHolder {
        TextView tv_chat_send_time;
    }
}
